package com.jd.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import com.jd.model.Response;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;

import com.jd.service.DepartmentService;
import com.jd.entity.Department;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangshuping
 * @since 2021-11-22
 */
@RestController
@Api(tags = "Department接口管理")
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    public DepartmentService departmentService;


    @GetMapping("/tree")
    @ApiOperation("部门树")
    public Response departmentTree() throws Exception {
        return Response.ok(departmentService.buildTree());
    }


    @GetMapping()
    @ApiOperation("获取列表")
    public Response list() throws Exception {
        return Response.ok(departmentService.list());
    }

    @GetMapping("/{key}")
    @ApiOperation("根据key获取")
    public Response getByKey(@PathVariable String key) throws Exception {
        return Response.ok(departmentService.getById(key));
    }

    @PostMapping
    @ApiOperation("保存")
    public Response save(@RequestBody Department department) throws Exception {
        boolean b = departmentService.save(department);
        return b ? Response.ok() : Response.fail();
    }

    @PutMapping
    @ApiOperation("修改")
    public Response update(@RequestBody Department department) throws Exception {
        boolean b = departmentService.updateById(department);
        return b ? Response.ok() : Response.fail();
    }

    @DeleteMapping("/{keys}")
    @ApiOperation("根据key值删除")
    public Response deleteByKeys(@PathVariable String[] keys) throws Exception {
        boolean b = departmentService.removeByIds(Arrays.asList(keys));
        return b ? Response.ok() : Response.fail();
    }
}
